package com.pwl.shiro;

import jdk.nashorn.internal.runtime.logging.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;

public class Main {
    public static void main(String[] args) {
        //工厂类：负责生产SecurityManager
        Factory<SecurityManager> factory=new IniSecurityManagerFactory("classpath:shiro.ini");
      //Shiro的核心：安全管理器
       SecurityManager securityManager=  factory.getInstance();
       //将完全管理器  添加到当前认证环境中
        SecurityUtils.setSecurityManager(securityManager);
        //认证主体： 用于保存用户输入的账号和密码
        Subject subject=SecurityUtils.getSubject();
        //token:令牌  存放了账号 和密码
        String username="zhangsan";
        String password="123456";
        UsernamePasswordToken token=new UsernamePasswordToken(username,password);
        try {
            subject.login(token);
            System.out.println("认证成功");
        } catch (AuthenticationException e) {
            //IncorrectCredentialsException 密码错误
            //UnknownAccountException  账号错误
            e.printStackTrace();
        }
    }
}
